package group2assignone.model;

import group2assignone.dao.MyDAO;
import group2assignone.ucm.UCStartup;

/**
 * models a booking
 * @author group2
 */
public class Booking
{
    private TicketComponent ticket;
    boolean multipleFlights = false;
    int bookingNum;
    boolean booked = false;

    public Booking()
    {
        MyDAO coms = UCStartup.getInstance().getDAO();
        this.bookingNum = coms.sQLGetLastBookingNumber() + 1;
        System.out.println("booking Number"+bookingNum);
    }


    public void addFlight(String flightNum, String username)
    {
        if (ticket == null) //only need single ticket
        {
            ticket = new Ticket(flightNum, username);
            
        }
        else if (!multipleFlights) //need ticket composite
        {
            Ticket t = (Ticket) ticket; //store first ticket
            ticket = new TicketComposite(); //make composite
            ticket.addTicket(t); //add original ticket
            multipleFlights = true;
            
            Ticket newTick = new Ticket(flightNum, username); //make new ticket
            //If for same flight, add promotion
            if(ticket.containsFlight(flightNum))
            {
                ticket.addTicket(new DiscountedTicket(newTick));
            }
            else
            {
                ticket.addTicket(newTick);
            }
        }
        else //already ticket composite -> just add flight
        {
            Ticket newTick = new Ticket(flightNum, username); //make new ticket
            
            //If for same flight, add promotion
            if(ticket.containsFlight(flightNum))
            {
                ticket.addTicket(new DiscountedTicket(newTick));
            }
            else
            {
                ticket.addTicket(newTick);
            }
        }
    }

    public void addFlight(String flightNum)
    {
        addFlight(flightNum, "Guest");

    }

    public int getBookingNum() {
        return bookingNum;
    }

    public boolean makeBooking()
    {
       ticket.bookTicket(bookingNum, 1);
       booked = ticket.isBooked();
       return booked;
    }
    
//    public void setTicketComponent(TicketComponent ticket)
//    {
//        this.ticket = ticket;
//    }
//
//    public TicketComponent getTicketComponent()
//    {
//        return ticket;
//    }
}
